package pers.sloera.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 最小绝对差
 *
 * @author sloera
 * @date 2022-07-04 08:38:08
 */
class MinimumAbsoluteDifference {
    public static void main(String[] args) {
        Solution solution = new MinimumAbsoluteDifference().new Solution();
        List<List<Integer>> lists = solution.minimumAbsDifference(new int[]{4, 2, 1, 3});
        System.out.println();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<List<Integer>> minimumAbsDifference(int[] arr) {
            Arrays.sort(arr);
            ArrayList<List<Integer>> lists = new ArrayList<>();
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < arr.length - 1; i++) {
                int diff = arr[i + 1] - arr[i];
                if (diff < min) {
                    lists.clear();
                    addListFromArr(arr, lists, i);
                    min = diff;
                } else if (diff == min) {
                    addListFromArr(arr, lists, i);
                }
            }
            return lists;
        }

        private void addListFromArr(int[] arr, ArrayList<List<Integer>> lists, int i) {
            ArrayList<Integer> operator = new ArrayList<>();
            operator.add(arr[i]);
            operator.add(arr[i + 1]);
            lists.add(operator);
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
